package com.eleadmin.gzh.controller;


import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.damon.utils.utils.HttpsUtils;
import com.eleadmin.gzh.service.GzhToken;
import io.swagger.annotations.Api;
import net.sf.json.JSONObject;
import org.dom4j.DocumentException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * api登录相关接口
 * Created by lining on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 * http://192.168.1.110/swagger-ui.html
 */
@RequestMapping("/api/gzh")
@RestController
@Api(value = "微信公众号", tags = {"微信公众号"})
public class GzhController extends BaseController {

    /**
     * 得到保存绑定用户二维码的ticket
     */
    @GetMapping(value = "getUserLoginTicket")
    public JsonResult getUserLoginTicket(String id) throws IOException, DocumentException {
        String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+ GzhToken.getAccessToken();
        //生成临时字符串二维码 expire_seconds 单位秒
        //{"scene_str": "customerId=12;customerName=中盈"}
        String data = "{\"expire_seconds\": 2000, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \""+id+"\"}}}";
        String post = HttpsUtils.post(url, data);
        String ticket = JSONObject.fromObject(post).getString("ticket");
        return JsonResult.ok().put("data", ticket);
    }

}
